import utils from '../utils/utils.js';
import {
	hex_md5
} from '@/common/js/md5.js'
import common from '../common/js/public.js'
// const baseUrl = 'https://grzq.wajx.top' //正式 2025-01-08
// const baseUrl = 'https://demogrzq.wajx.top' //正式 2025-01-08
const baseUrl = 'http://127.0.0.1:6011' //本地  2025-01-08
//
//封装请求接口函数

function request(params)  {
	return new Promise((resolve, reject) => {
		params.data = params.data || {};
		//拼接url
		params.url = baseUrl + params.url;
		params.loading = params.loading == undefined ? true : params.loading;

		params.method = params.method || "POST"
		params.method = params.method.toUpperCase(); //小写改为大写
		//显示加载动画
		if (params.loading) utils.showLoading(); //s

		let header;

		//此项目token放入header中
		let token = uni.getStorageSync('token') ? uni.getStorageSync('token') : '';
		console.log("uni.getStorageSync('token')")
		console.log(uni.getStorageSync('token'))
		if (params.method == "POST") {
			header = {
				'content-type': "application/x-www-form-urlencoded",
				'token': token,
			};
		} else {
			header = {
				'content-type': "application/json",
				'token': token,
			};
		}

		uni.request({
			url: params.url,
			method: params.method,
			data: params.data,
			header: header,
			success: res => {
				// if (params.loading) utils.hideLoading();
				let code = res.statusCode;
				let message = res.data.message;
				let overdue = true
				// alert('code'+res.data.code)

				// resolve(res.data)

				// 统一处理返回值 待解开注释
				switch (code) {
					case 200:
						// alert(200)
						resolve(res.data)
						break
					case 1002:

						break;
					case 401:
					case 406:
						overdue = false
					case 202:
						// case 1002:
					case 2003:

					case 403:

						utils.showToast(message, "none")
						// }, 0)
						reject(res)


						// utils.goLogin()
						break
					case 422:
						// setTimeout(() => {
						utils.showToast(message, "none")
						// }, 0)
						reject(res)


						// utils.goLogin()
						break
					case 1001:
					case 423:
						utils.showToast(message, "none")

						// utils.goLogin()
						reject(res)
						break
					default:
						// alert('default')
						// setTimeout(() => {
						utils.showToast(message, "none")
						// }, 0)

						// resolve(res.data);
						resolve(message);
				}
				//统一处理返回值 待解开注释


			},
			fail: err => {
				reject(err)
			},
			complete: () => {
				//结束加载动画
				utils.hideLoading();
			}
		});
	})
}

export {
	request,
	baseUrl,
}
